﻿using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using AotoFacDal.User;
using AotoFacLogic.User;
using Autofac;
using Autofac.Integration.WebApi;
using IAotoFacLogic.User;
using IAutoFacDal.User;

namespace AotoFacWebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AutoFacStart();
        }
        #region 实现依赖注入
        /// <summary>
        /// 使用AutoFac实现依赖注入，
        /// </summary>
        public void AutoFacStart()
        {
            var builder = new ContainerBuilder();
            SetupResolveRules(builder);  //注入
         //   builder.RegisterApiControllers(Assembly.GetExecutingAssembly());  //注入Controller

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());//注入APi
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);//注入APi
            builder.RegisterWebApiModelBinderProvider();//注入APi
            var container = builder.Build();
           // DependencyResolver.SetResolver(new AutofacWebApiDependencyResolver(container));//注入Controller
            var webApiResolver = new AutofacWebApiDependencyResolver(container);    //注入APi
            GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;  //注入APi
        }

        //public  void CoreAutoFacInit()
        //{
        //    var builder = new ContainerBuilder();
        //    HttpConfiguration config = GlobalConfiguration.Configuration;

        //    SetupResolveRules(builder);

        //    ////注册所有的Controllers
        //    //builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        //    //注册所有的ApiControllers
        //    builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();

        //    var container = builder.Build();
        //    //注册api容器需要使用HttpConfiguration对象
        //    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        //    DependencyResolver.SetResolver(new AutofacWebApiDependencyResolver(container));
        //}

        /// <summary>
        /// 注入规则
        /// </summary>
        /// <param name="builder"></param>
        protected void SetupResolveRules(ContainerBuilder builder)
        {

            #region 1.  UI项目只用引用service和repository的接口，不用引用实现的dll。
            //如需加载实现的程序集，将dll拷贝到bin目录下即可，不用引用dll
            var IServicesBLL = Assembly.Load("IAotoFacLogic");
            var ServicesBLL = Assembly.Load("AotoFacLogic");

            var IServicesDAL = Assembly.Load("IAutoFacDal");
            var ServicesDAL = Assembly.Load("AotoFacDal");

            //实现服务接口和服务实现的依赖 业务逻辑层
            builder.RegisterAssemblyTypes(IServicesBLL, ServicesBLL).AsImplementedInterfaces();

            //实现服务接口和服务实现的依赖 数据层
            builder.RegisterAssemblyTypes(IServicesDAL, ServicesDAL).AsImplementedInterfaces();
            #endregion

            #region 2. 特定条件的过滤注册-只注册结尾是User的
            //builder.RegisterAssemblyTypes(IServices, Services).Where(t => t.Name.EndsWith("User")).AsImplementedInterfaces(); 
            #endregion RegisterType
            //使用新的覆盖旧的注册DLL-使用UserNew代替UserOld
            //builder.RegisterType<UserLogic>().As<IUserLogic>();
            //builder.RegisterType<UserDal>().As<IUserDal>();
        }
        #endregion
    }
}
